package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.OrderDetailsModel;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.OrderDetailsContract;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.presenter.OrderDetailsPresenter;
import com.shd.edanpei.ui.view.AuditDataView;
import com.shd.edanpei.ui.view.MeCustomerView;
import com.shd.edanpei.ui.view.MeIntegralView;
import com.shd.edanpei.ui.view.MeOrderStatusView;
import com.shd.edanpei.ui.view.OrderInfoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/21.
 * 我的订单详情
 */

public class MeOrderDetailsActivity extends BaseActivity<OrderDetailsPresenter> implements OrderDetailsContract.OrderDetailsIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.tv_cancel)
    View cancel;
    @BindView(R.id.tv_unsure)
    View unSure;
    @BindView(R.id.tv_sure)
    View sure;
    @BindView(R.id.tv_remove)
    View remove;
    @BindView(R.id.tv_goCreate)
    View goCreate;
    @BindView(R.id.tv_updata)
    View updata;
    //    @BindView(R.id.tv_sureUpdate)
//    View sureUpdate;
    @BindView(R.id.rl_button)
    View buttons;
    @BindView(R.id.tv_service)
    View payService;

    private OrderDetailsModel model;
    private int orderID;
    private int position;
    private int orderType;


    @Override
    protected int setLayout() {
        return R.layout.activity_meorderdetails;
    }

    @Override
    protected OrderDetailsPresenter bindPresenter() {
        return new OrderDetailsPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("我的订单");


        Bundle extras = getIntent().getExtras();
        orderID = extras.getInt(Constant.ORDER_ID);
        position = extras.getInt(Constant.INDEX);
        orderType = extras.getInt(Constant.ORDER_TYPE);
        mPresenter.requetData(orderID);
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        finish();
    }


    @Override
    public void setData(OrderDetailsModel model) {
        this.model = model;
        container.addView(new MeOrderStatusView(this, model));
        container.addView(new AuditDataView(this, model, Constant.ME_ORDER));
        container.addView(new MeCustomerView(this, model));
        if(StatusEnum.getCDes(model.getBaseInfo().getUserStatus()).equals("已完成"))
        container.addView(new MeIntegralView(this,model));
        container.addView(new OrderInfoView(this, model));
        initButton();

    }
    @OnClick(R.id.tv_service)
    void toService(){
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.ORDER_ID, orderID);
        startActivity(SureLoanInfoActivity.class, bundle);
    }

    @Subscribe(code = RxBusCode.UPDATA_PHOTO_SUCCESS)
    void toFinish(){
        finish();
    }

    private void initButton() {
        cancel.setVisibility(View.GONE);
        unSure.setVisibility(View.GONE);
        sure.setVisibility(View.GONE);
        goCreate.setVisibility(View.GONE);
        remove.setVisibility(View.GONE);
        updata.setVisibility(View.GONE);
        int status = model.getBaseInfo().getUserStatus();
        if (status == StatusEnum.unpaid.getStatus()) {
            //未支付
            cancel.setVisibility(View.VISIBLE);
//            pay.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.applying.getStatus()) {
            //已支付，未被接单
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.first.getStatus()) {
            //已被接单，未初审
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.last.getStatus()) {
            //已初审，未终审核
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitService.getStatus()) {
            //已终审，未支付服务费
            cancel.setVisibility(View.VISIBLE);
            payService.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitLoan.getStatus()) {
            unSure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitfirm.getStatus()) {
            //已放款
           sure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.success.getStatus()) {
            //交易成功
            buttons.setVisibility(View.GONE);
        } else if (status == StatusEnum.error.getStatus()) {
            //订单超时，无人接单
            cancel.setVisibility(View.VISIBLE);
            goCreate.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.cancel.getStatus()) {
            //未支付，撤销订单
            remove.setVisibility(View.VISIBLE);
            updata.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.refuse.getStatus()) {
            //被拒绝
            cancel.setVisibility(View.VISIBLE);
//            goCreate.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitRepay.getStatus()) {
            //已支付，撤销订单,退款中
            updata.setVisibility(View.VISIBLE);
            if (model.getManager() != null && model.getManager().getId() > 0) {

            } else {
                updata.setVisibility(View.VISIBLE);
            }
        } else if (status == StatusEnum.repaySuccess.getStatus()) {
            //退款成功
            updata.setVisibility(View.VISIBLE);
            remove.setVisibility(View.VISIBLE);
        }

    }

    /**
     * 撤销订单
     */
    @OnClick(R.id.tv_cancel)
    void onCanle() {
        mPresenter.userUndo(orderID, orderType, position, model.getBaseInfo().isPayDeposit());
    }

    /**
     * 修改
     */
    @OnClick(R.id.tv_updata)
    void onUpdate() {
        if (model != null) {
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.ORDER_ID, orderID);
            bundle.putInt(Constant.ORDER_TYPE, orderType);
            bundle.putInt(Constant.INDEX, position);
            bundle.putString(Constant.ADDRESS, model.getBaseInfo().getArea());
            bundle.putInt(Constant.CUSTOMER_ID, model.getBaseInfo().getCreateUserId());
            mPresenter.updataOrder(bundle, orderID, model.getBaseInfo().getUserStatus());
        }
    }


    /**
     * 立即确认
     */
    @OnClick(R.id.tv_sure)
    void onSure() {
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.ORDER_ID, orderID);
        bundle.putInt(Constant.ORDER_TYPE, orderType);
        bundle.putInt(Constant.INDEX, position);
        mPresenter.sureGetLone(bundle);
    }
    @OnClick(R.id.tv_unsure)
    void onUnSure(){
//        Bundle bundle = new Bundle();
//        bundle.putInt(Constant.ORDER_ID, orderID);
//        bundle.putInt(Constant.ORDER_TYPE, orderType);
//        bundle.putInt(Constant.INDEX, position);
//        mPresenter.sureGetLone(bundle);
    }

    /**
     * 重新申请
     */
    @OnClick(R.id.tv_goCreate)
    void onRe() {
        mPresenter.againApply(orderID, orderType, position);
    }

    /**
     * 移除订单
     */
    @OnClick(R.id.tv_remove)
    void onRemove() {
        mPresenter.remove(orderID, orderType, position, Constant.ME_ORDER);
    }


    //刷新列表
    @Subscribe(code = RxBusCode.REFRESH_ORDER_STATUS)
    public void refreshStatus(StatusBean bean) {
        model.getBaseInfo().setUserStatus(bean.getOrderStatus());
        initButton();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        container.removeAllViews();
        super.onDestroy();
    }

}
